var IsRevisitInfoDlg = {isRevisitInfoData: {}, zTreeInstance: null, validateFields: {}};
//清除数据
IsRevisitInfoDlg.clearData = function () {
    this.isRevisitInfoData = {};
};
//设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
IsRevisitInfoDlg.set = function (key, val) {
    this.isRevisitInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
};
//设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
IsRevisitInfoDlg.get = function (key) {
    return $("#" + key).val();
};
//关闭此对话框
IsRevisitInfoDlg.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};

IsRevisitInfoDlg.showIsRevisitSelectTree = function () {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentIsRevisitMenu").css({
        left: pNameOffset.left + "px",
        top: pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");
    $("body").bind("mousedown", onBodyDown);
};

IsRevisitInfoDlg.hideIsRevisitSelectTree = function () {
    $("#parentIsRevisitMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};
//收集数据
IsRevisitInfoDlg.collectData = function () {
    this.set('id').set('status').set('follower').set('customerPhone').set('belongers').set('isEffective').set('followers').set('statuss').set('reservationsId').set('revisitContent').set('isReturnVisit').set('reservations').set('remarksOfReturnVisit').set('causeOfNoReturnVisit').set('appointmentTime');
};
//验证数据是否为空
IsRevisitInfoDlg.validate = function () {
    $('#isRevisitInfoForm').data("bootstrapValidator").resetForm();
    $('#isRevisitInfoForm').bootstrapValidator('validate');
    return $("#isRevisitInfoForm").data('bootstrapValidator').isValid();
};

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentIsRevisitMenu" || $(
        event.target).parents("#parentIsRevisitMenu").length > 0)) {
        IsRevisitInfoDlg.hideIsRevisitSelectTree();
    }
}

IsRevisitInfoDlg.outbounds = function (customerPhone) { // 外呼
    var index = layer.open({
        type: 2,
        title: '外呼',
        area: ['50%', '50%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservation/to_outbounds/' + customerPhone +'/' +document.getElementById("id").value
    });
    this.layerIndex = index;
};

IsRevisitInfoDlg.hangup = function () { // 挂断
    $.ajax({
        url: parent.parent.document.getElementById("whurl").value,
        type: "get",
        data: {
            "op": "onhook",
            "Exten": parent.parent.document.getElementById("exten").value
        },
        success: function (data) {
            if(data==100){
                Feng.success('挂断成功!');
            } else if(data==101){
                Feng.success('分机号不存在!');
            } else if(data==103){
                Feng.success('参数错误!');
            } else if(data==500){
                Feng.success('其他错误!');
            }
        }
    })
};

//已到添加回访
IsRevisitInfoDlg.addIsRevisitsdds = function () {
    this.clearData();
    this.collectData();
    if (document.getElementById("appointmentTime").value == null || document.getElementById("appointmentTime").value == "") {
        var selectStr = $("#statuss").val();
        if (selectStr != '过期' && selectStr != '无效') {
            alert("请输入预约回访时间");
            return;
        }
    }
    if (document.getElementById("revisitContent").value == null || document.getElementById("revisitContent").value == "") {
        alert("请输入回访记录");
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/reservations/add_isRevisits", function (data) {
        if (window.parent.document.getElementById("pageType").value == 2) {
            Feng.success("操作成功!");
            window.parent.ReservationsNoArrivedTable.getScrollPosition();
            window.parent.ReservationsNoArrivedTable.search();
            window.parent.ReservationsNoArrivedTable.scrollTo();
            window.location.reload();
        }
        if (window.parent.document.getElementById("pageType").value == 1) {
            Feng.success("操作成功!");
            window.parent.ReservationsArrivedTable.getScrollPosition();
            window.parent.ReservationsArrivedTable.search();
            window.parent.ReservationsArrivedTable.scrollTo();
            window.location.reload();
        }
        if (window.parent.document.getElementById("pageType").value == 0) {
            Feng.success("操作成功!");
            window.parent.ReservationsRecordTable.getScrollPosition();
            window.parent.ReservationsRecordTable.search();
            window.parent.ReservationsRecordTable.scrollTo();
            window.location.reload();
        }
        if (window.parent.document.getElementById("pageType").value == 4) {
            Feng.success("操作成功!");
            window.parent.RevisitRecord.getScrollPosition();
            window.parent.RevisitRecord.search();
            window.parent.RevisitRecord.scrollTo();
            window.location.reload();
        }
        if (window.parent.document.getElementById("pageType").value == 5) {
            Feng.success("操作成功!");
            window.parent.ReservationsMyWorkTable.getScrollPosition();
            window.parent.ReservationsMyWorkTable.search();
            window.parent.ReservationsMyWorkTable.scrollTo();
            window.location.reload();
        }
    }, function (data) {
        alert("没有权限，请联系系统管理员！");
        window.location.reload();
    });
    ajax.set(this.isRevisitInfoData);
    ajax.start();
};

//未到添加回访
IsRevisitInfoDlg.addIsRevisitsddsyd = function () {
    this.clearData();
    this.collectData();
    if (document.getElementById("appointmentTime").value == null || document.getElementById("appointmentTime").value == "") {
        var selectStr = $("#statuss").val();
        if (selectStr != '过期' && selectStr != '无效') {
            alert("请输入预约回访时间");
            return;
        }
    }
    if (document.getElementById("revisitContent").value == null || document.getElementById("revisitContent").value == "") {
        alert("请输入回访记录");
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/reservations/add_isRevisits", function (data) {
        if (window.parent.document.getElementById("pageType").value == 2) {
            Feng.success("操作成功!");
            window.parent.ReservationsNoArrivedTable.getScrollPosition();
            window.parent.ReservationsNoArrivedTable.search();
            window.parent.ReservationsNoArrivedTable.scrollTo();
            window.location.reload();
        }
        if (window.parent.document.getElementById("pageType").value == 1) {
            Feng.success("操作成功!");
            window.parent.ReservationsArrivedTable.getScrollPosition();
            window.parent.ReservationsArrivedTable.search();
            window.parent.ReservationsArrivedTable.scrollTo();
            window.location.reload();
        }
        if (window.parent.document.getElementById("pageType").value == 0) {
            Feng.success("操作成功!");
            window.parent.ReservationsRecordTable.getScrollPosition();
            window.parent.ReservationsRecordTable.search();
            window.parent.ReservationsRecordTable.scrollTo();
            window.location.reload();
        }
        if (window.parent.document.getElementById("pageType").value == 4) {
            Feng.success("操作成功!");
            window.parent.RevisitRecord.getScrollPosition();
            window.parent.RevisitRecord.search();
            window.parent.RevisitRecord.scrollTo();
            window.location.reload();
        }
        if (window.parent.document.getElementById("pageType").value == 5) {
            Feng.success("操作成功!");
            window.parent.ReservationsMyWorkTable.getScrollPosition();
            window.parent.ReservationsMyWorkTable.search();
            window.parent.ReservationsMyWorkTable.scrollTo();
            window.location.reload();
        }
    }, function (data) {
        alert("没有权限，请联系系统管理员！");
        window.location.reload();
    });
    ajax.set(this.isRevisitInfoData);
    ajax.start();
};

//新建预约
function subsequent(id) {
    var index = layer.open({
        type: 2,
        title: '复诊预约',
        area: ['95%', '95%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/reservationsNotArrive/marksubsequent_info/' + id + '/1'
    });
    this.layerIndex = index;
};

$(function () {
    var seletestatuss = $('#statussValue').val();
    if ($('#seletestatuss').val() != "" && seletestatuss != "") {
        $("#statuss option[value='" + seletestatuss + "']").attr("selected", "selected");
    }
    if ($("#statuss").val() == "过期") {
        var data = new Date();
        var y = data.getFullYear();
        var m = data.getMonth() + 1;
        var d = data.getDate();
        if (m < 10) {
            m = "0" + m
        }
        if (d < 10) {
            d = "0" + d
        }
        var time = y + "-" + m + "-" + d;
        $("#appointmentTime").val(time)
    }
    if ($("#statuss").val() == "无效") {
        $("#appointmentTime").val("")
    }
    Feng.initValidator("isRevisitInfoForm", IsRevisitInfoDlg.validateFields);
    var radios = document.getElementsByName('radio');
    for (var i = 0; i < radios.length; i++) {
        radios[i].indexs = i + 1;
        radios[i].onchange = function () {
            document.getElementById('url1').value = '';
            if (this.checked) {
                document.getElementById("url2").style.display = "none";
                document.getElementById("url1").style.display = "none";
                document.getElementById("causeOfNoReturnVisit").value = "";
                document.getElementById("remarksOfReturnVisit").value = "";
                document.getElementById("appointmentTime").value = "";
                document.getElementById('url1').selectedIndex = "";
                document.getElementById('url2').value = '';
                document.getElementById("url" + this.indexs).style.display = "block";
            }
        }
    }
});